#import "RNAddListItemCell.h"
#import "RNConstants.h"

static NSColor *highlightColor;

@implementation RNAddListItemCell

@synthesize favorite = favorite_;
@synthesize category = category_;
@synthesize titleAttributes = titleAttributes_;
@synthesize selectedTitleAttributes = selectedTitleAttributes_;
@synthesize categoryAttributes = categoryAttributes_;
@synthesize selectedCategoryAttributes = selectedCategoryAttributes_;

+(void)initialize {
    highlightColor = [NSColor colorWithCalibratedRed:0.17 green:0.409 blue:0.793 alpha:1.0f];
}

-(void)setObjectValue:(id)obj {
    if (![obj isKindOfClass:[NSDictionary class]])
        return;
    [self setCategory:[obj valueForKey:@"category"]];
    [self setFavorite:[obj valueForKey:@"favorite"]];
    [super setObjectValue:[obj valueForKey:RNTitleKey]];
}

-(NSDictionary *)titleAttributes {
    if (!titleAttributes_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont controlContentFontOfSize:12.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
        titleAttributes_ = attributes;
    }
    return titleAttributes_;
}

-(NSDictionary *)selectedTitleAttributes {
    if (!selectedTitleAttributes_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont fontWithName:@"LucidaGrande-Bold" size:12.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
        selectedTitleAttributes_ = attributes;
    }
    return selectedTitleAttributes_;
}

-(NSDictionary *)categoryAttributes {
    if (!categoryAttributes_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont controlContentFontOfSize:10.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor darkGrayColor] forKey:NSForegroundColorAttributeName];
        categoryAttributes_ = attributes;
    }
    return categoryAttributes_;
}

-(NSDictionary *)selectedCategoryAttributes {
    if (!selectedCategoryAttributes_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont controlContentFontOfSize:10.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor colorWithCalibratedWhite:0.9f alpha:1.0f] forKey:NSForegroundColorAttributeName];
        selectedCategoryAttributes_ = attributes;
    }
    return selectedCategoryAttributes_;
}

-(NSAttributedString *)attributedStringValue {
    return [[NSAttributedString alloc] initWithString:[self stringValue] attributes:[self isHighlighted] ? [self selectedTitleAttributes] : [self titleAttributes]];
}

-(NSAttributedString *)categoryAttributedString {
    id myCategory = [self category];
    return [[NSAttributedString alloc] initWithString:myCategory == [NSNull null] ? @"No category assigned" : [myCategory title] attributes:[self isHighlighted] ? [self selectedCategoryAttributes] : [self categoryAttributes]];
}


static CGFloat titleLeftPadding = 14.0f;

-(NSRect)titleRectForBounds:(NSRect)cellFrame {
    return NSMakeRect(NSMinX(cellFrame) + titleLeftPadding, NSMinY(cellFrame), NSWidth(cellFrame) - titleLeftPadding, [[self attributedStringValue] size].height);
}

-(NSRect)categoryRectForBounds:(NSRect)cellFrame {
    NSRect titleRect = [self titleRectForBounds:cellFrame];
    NSRect categoryRect = NSMakeRect(NSMinX(titleRect), NSMaxY(titleRect), NSWidth(titleRect), NSHeight(cellFrame) - NSHeight(titleRect));
    return categoryRect;
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    if ([self isHighlighted]) {
        NSRect highlightRect = NSInsetRect(cellFrame, 4.0f, 0.0f);
        NSBezierPath *highlightPath = [NSBezierPath bezierPathWithRoundedRect:highlightRect xRadius:RNDetailViewCellCornerRadius yRadius:RNDetailViewCellCornerRadius];
        [highlightColor setFill];
        [highlightPath fill];
    }
    [[self attributedStringValue] drawInRect:[self titleRectForBounds:cellFrame]];
//    NSAttributedString *categoryString = [[NSAttributedString alloc] initWithString:[[self category] title] attributes:[self categoryAttributes]];
    [[self categoryAttributedString] drawInRect:[self categoryRectForBounds:cellFrame]];
}

@end
